package com.ruoyi.phoneController;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.domain.MobileCoupon;
import com.ruoyi.service.IMobileCouponService;

/**
 * 手机端商家优惠券Controller
 * 
 * @author ruoyi
 * @date 2025-01-20
 */
@RestController
@RequestMapping("/mobile/coupon")
@CrossOrigin(origins = "*", maxAge = 3600)
public class MobileCouponController extends BaseController
{
    @Autowired
    private IMobileCouponService mobileCouponService;

    /**
     * 查询商家优惠券列表
     */
    @GetMapping("/list")
    public AjaxResult list(MobileCoupon mobileCoupon)
    {
        try
        {
            List<MobileCoupon> list = mobileCouponService.selectMobileCouponList(mobileCoupon);
            return success(list);
        }
        catch (Exception e)
        {
            logger.error("查询商家优惠券列表失败", e);
            return error("查询失败");
        }
    }

    /**
     * 获取商家优惠券详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        if (id == null || id <= 0)
        {
            return error("优惠券ID不能为空");
        }

        try
        {
            MobileCoupon coupon = mobileCouponService.selectMobileCouponById(id);
            if (coupon == null)
            {
                return error("优惠券不存在");
            }
            return success(coupon);
        }
        catch (Exception e)
        {
            logger.error("获取优惠券详情失败", e);
            return error("获取优惠券详情失败");
        }
    }

    /**
     * 根据商家ID查询可用优惠券列表
     */
    @GetMapping("/available/{merchantId}")
    public AjaxResult getAvailableCouponsByMerchant(@PathVariable("merchantId") Integer merchantId)
    {
        if (merchantId == null || merchantId <= 0)
        {
            return error("商家ID不能为空");
        }

        try
        {
            List<MobileCoupon> list = mobileCouponService.selectAvailableCouponsByMerchantId(merchantId);
            return success(list);
        }
        catch (Exception e)
        {
            logger.error("查询商家可用优惠券失败", e);
            return error("查询失败");
        }
    }

    /**
     * 根据商家ID查询用户可领取的优惠券列表
     */
    @GetMapping("/receivable/{merchantId}/{userId}")
    public AjaxResult getReceivableCouponsByMerchant(@PathVariable("merchantId") Integer merchantId, 
                                                    @PathVariable("userId") Integer userId)
    {
        if (merchantId == null || merchantId <= 0)
        {
            return error("商家ID不能为空");
        }
        
        if (userId == null || userId <= 0)
        {
            return error("用户ID不能为空");
        }

        try
        {
            List<MobileCoupon> list = mobileCouponService.selectReceivableCouponsByMerchantId(merchantId, userId);
            return success(list);
        }
        catch (Exception e)
        {
            logger.error("查询用户可领取优惠券失败", e);
            return error("查询失败");
        }
    }

    /**
     * 新增商家优惠券
     */
    @PostMapping
    public AjaxResult add(@RequestBody MobileCoupon mobileCoupon)
    {
        try
        {
            return toAjax(mobileCouponService.insertMobileCoupon(mobileCoupon));
        }
        catch (Exception e)
        {
            logger.error("新增优惠券失败", e);
            return error("新增失败");
        }
    }

    /**
     * 修改商家优惠券
     */
    @PutMapping
    public AjaxResult edit(@RequestBody MobileCoupon mobileCoupon)
    {
        try
        {
            return toAjax(mobileCouponService.updateMobileCoupon(mobileCoupon));
        }
        catch (Exception e)
        {
            logger.error("修改优惠券失败", e);
            return error("修改失败");
        }
    }

    /**
     * 删除商家优惠券
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        try
        {
            return toAjax(mobileCouponService.deleteMobileCouponByIds(ids));
        }
        catch (Exception e)
        {
            logger.error("删除优惠券失败", e);
            return error("删除失败");
        }
    }
}
